//
//  HKNavigationBar.m
//  HKTimeKitApp
//
//  Created by huzhaoke1 on 2023/5/10.
//

#import "HKNavigationBar.h"
#import "HKUITool.h"
#import "UIView+properties.h"
#import <Masonry/Masonry.h>
#import "UILabel+create.h"
#import "HKFontHelper.h"
#import "HKCommonDefine.h"

static const CGFloat kKSContainerH = 44.f;
static const CGFloat kKSLeftMargin = 12.f;

@interface HKNavigationBar ()

@property (nonatomic, strong) UIView *bgView;
@property (nonatomic, strong) UIView *containerView;
@property (nonatomic, strong) UIImageView *backImageView;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) CALayer *bgLayer;

@end

@implementation HKNavigationBar

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor whiteColor];
        [self setupSubViews];
    }
    return self;
}

- (void)setupSubViews {
    [self addSubview:self.bgView];
    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsMake(0, 0, 0, 0));
    }];
    [self addSubview:self.containerView];
    [self.containerView addSubview:self.backImageView];
    [self.containerView addSubview:self.titleLabel];
    CGFloat top = [HKUITool getIPhoneStatusBarHeight];
    [self.containerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(top);
        make.width.mas_equalTo(self);
        make.height.mas_equalTo(kKSContainerH);
    }];
    [self.backImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(kKSLeftMargin);
        make.centerY.mas_equalTo(self.containerView);
    }];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(50);
        make.right.mas_equalTo(self.containerView).offset(-50);
        make.centerY.mas_equalTo(self.containerView);
    }];
}

- (void)updateHiddenBackButton:(BOOL)hidden {
    self.backImageView.hidden = hidden;
}

- (void)updateBackgroundColor:(UIColor *)backgroundColor {
    self.backgroundColor = backgroundColor;
    self.containerView.backgroundColor = backgroundColor;
}

+ (CGFloat)naviBarHeight {
    return [HKUITool getIPhoneStatusBarHeight] + kKSContainerH;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    self.height = [[self class] naviBarHeight];
    self.layer.frame = self.bgView.bounds;
}

- (void)updateBackImageName:(NSString *)imageName {
    [self.backImageView setImage:[UIImage imageNamed:imageName]];
}

- (void)updateTitle:(NSString *)title {
    self.titleLabel.text = title;
}

- (void)updateTitleColor:(UIColor *)titleColor {
    self.titleLabel.textColor = titleColor;
}

- (void)updateGradienColorFromColor:(UIColor *)fromColor toColor:(UIColor *)toColor {
    // gradient
    CAGradientLayer *gl = [CAGradientLayer layer];
    gl.frame = CGRectMake(0, 0, HKScreenWidth, [[self class] naviBarHeight]);
    gl.startPoint = CGPointMake(0.5, -0.3);
    gl.endPoint = CGPointMake(0.5, 0.69);
    gl.colors = @[(__bridge id)fromColor.CGColor, (__bridge id)toColor.CGColor];
    gl.locations = @[@(0), @(1.0f)];
    self.bgLayer = gl;
    [self.bgView.layer addSublayer:self.bgLayer];
}

#pragma mark - tapGesture
- (void)onTapBack:(id)sender {
    if ([self.delegate respondsToSelector:@selector(didTapBack)]) {
        [self.delegate didTapBack];
    }
}

#pragma mark - getter & setter
- (UIView *)containerView {
    if (!_containerView) {
        _containerView = [UIView new];
        _containerView.backgroundColor = [UIColor clearColor];
    }
    return _containerView;
}

- (UIView *)bgView {
    if (!_bgView) {
        _bgView = [UIView new];
    }
    return _bgView;
}

- (UIImageView *)backImageView {
    if (!_backImageView) {
        _backImageView = [UIImageView new];
        _backImageView.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onTapBack:)];
        [_backImageView addGestureRecognizer:tap];
    }
    return _backImageView;
}

- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [UILabel labelWithFont:[HKFontHelper appcnBoldFontWithSize:16]
                                     bgColor:nil
                                   textColor:UIColorFromRGB(0x4448FF) textAligment:NSTextAlignmentCenter];
    }
    return _titleLabel;
}

@end
